<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务状态" prop="pend">
        <el-input
          v-model="queryParams.pend"
          placeholder="请输入任务状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="优先级" prop="orderLevel">
        <el-select v-model="queryParams.orderLevel" placeholder="请选择优先级">
          <el-option label="全部" value=""></el-option>
          <el-option label="普通" value="普通"></el-option>
          <el-option label="紧急" value="紧急"></el-option>
          <el-option label="非常紧急" value="非常紧急"></el-option>
        </el-select>
      </el-form-item>



      <el-form-item label="车间" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入车间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入生产线"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-select v-model="queryParams.process" placeholder="请选择工序">
          <el-option label="全部" value=""></el-option>
        <el-option label="制造" value="制造"></el-option>
        <el-option label="加工" value="加工"></el-option>
        <el-option label="包装" value="包装"></el-option>
      </el-select>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['process:process:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['process:process:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['process:process:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['process:process:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label=" 任务单编号" align="center" prop="processId" />
      <el-table-column label="任务状态" align="center" prop="pend" />
      <el-table-column label="生产批次" align="center" prop="batch" />
      <el-table-column label="优先级" align="center" prop="orderLevel" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.orderLevel==='普通'">普通</el-tag>
          <el-tag v-if="scope.row.orderLevel==='紧急'">紧急</el-tag>
          <el-tag v-if="scope.row.orderLevel==='非常紧急'">非常紧急</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="工序名称" align="center" prop="processName" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品编码" align="center" prop="productCode" />
      <el-table-column label="工单数量" align="center" prop="plannedProduction" />
      <el-table-column label=" 报工数量" align="center" prop="reportedQuantity" />
      <el-table-column label="车间" align="center" prop="shopId" >
        <template slot-scope="scope">

          <el-tag v-if="scope.row.shopId===1">机械制造车间</el-tag>
          <el-tag v-if="scope.row.shopId===2">组装车间</el-tag>
          <el-tag v-if="scope.row.shopId===3">包装车间</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="生产线" align="center" prop="productId">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.productId===1">机械加工生产线A</el-tag>
          <el-tag v-if="scope.row.productId===2">机械加工生产线B</el-tag>
          <el-tag v-if="scope.row.productId===3">机械加工生产线C</el-tag>
          <el-tag v-if="scope.row.productId===4">机械加工生产线D</el-tag>
          <el-tag v-if="scope.row.productId===5">机械加工生产线E</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="工艺" align="center" prop="craftId">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.craftId===1">精益制造工艺</el-tag>
          <el-tag v-if="scope.row.craftId===2">包装工艺010</el-tag>
          <el-tag v-if="scope.row.craftId===3">焊接加工通用工艺</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上线时间" align="center" prop="startdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下线时间" align="center" prop="denddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.denddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creator" />
      <el-table-column label="创建时间" align="center" prop="creationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['process:process:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['process:process:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序任务单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-bod y>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务状态" prop="pend">
          <el-input v-model="form.pend" placeholder="请输入任务状态" />
        </el-form-item>
        <el-form-item label="生产批次" prop="batch">
          <el-input v-model="form.batch" placeholder="请输入生产批次" />
        </el-form-item>
        <el-form-item label="优先级" prop="orderLevel">
          <el-input v-model="form.orderLevel" placeholder="请输入优先级" />
        </el-form-item>
        <el-form-item label="工序名称" prop="processName">
          <el-input v-model="form.processName" placeholder="请输入工序名称" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品编码" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="工单数量" prop="plannedProduction">
          <el-input v-model="form.plannedProduction" placeholder="请输入工单数量" />
        </el-form-item>
        <el-form-item label=" 报工数量" prop="reportedQuantity">
          <el-input v-model="form.reportedQuantity" placeholder="请输入 报工数量" />
        </el-form-item>
        <el-form-item label="车间" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入车间" />
        </el-form-item>
        <el-form-item label="生产线" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入生产线" />
        </el-form-item>
        <el-form-item label="工艺" prop="craftId">
          <el-input v-model="form.craftId" placeholder="请输入工艺" />
        </el-form-item>
        <el-form-item label="上线时间" prop="startdate">
          <el-date-picker clearable
            v-model="form.startdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上线时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下线时间" prop="denddate">
          <el-date-picker clearable
            v-model="form.denddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下线时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="creationTime">
          <el-date-picker clearable
            v-model="form.creationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcess, getProcess, delProcess, addProcess, updateProcess } from "@/api/process/process";

export default {
  name: "Process",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序任务单表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pend: null,
        batch: null,
        orderLevel: null,
        processName: null,
        productName: null,
        productCode: null,
        plannedProduction: null,
        reportedQuantity: null,
        shopId: null,
        productId: null,
        craftId: null,
        startdate: null,
        denddate: null,
        creator: null,
        creationTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工序任务单列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        processId: null,
        pend: null,
        batch: null,
        orderLevel: null,
        processName: null,
        productName: null,
        productCode: null,
        plannedProduction: null,
        reportedQuantity: null,
        shopId: null,
        productId: null,
        craftId: null,
        startdate: null,
        denddate: null,
        creator: null,
        creationTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.processId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序任务单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const processId = row.processId || this.ids
      getProcess(processId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工序任务单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.processId != null) {
            updateProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const processIds = row.processId || this.ids;
      this.$modal.confirm('是否确认删除工序任务单编号为"' + processIds + '"的数据项？').then(function() {
        return delProcess(processIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('process/process/export', {
        ...this.queryParams
      }, `process_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
